package com.example.resule;

import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @author zhanghuaxi
 * @author Administrator
 * @program: 放回结构集
 * @description: 放回结构集
 * @date 2020-12-10 15:46:29
 */
@Data
@ToString
@Slf4j
public class Result<T> {
    /**
     * 提示码
     */
    private Integer code;
    /**
     * 返回是否成功o
     */
    private Boolean success;
    /**
     * 提示信息
     */
    private String message;

    /**
     * 具体的内容
     */
    private T data;

    private Map<String, Object> tomap;


    public Result() {
    }


    /**
     * 成功时候的调用
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T data) {
        Result r = new Result();
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS);
        r.setMessage("true");
        r.data = data;
        return r;
    }

    public static <T> Result<T> success(T data, CustomizeResultCode customizeResultCode) {
        Result r = new Result();
        r.setSuccess(true);
        r.setCode(customizeResultCode.getCode());
        r.setMessage(customizeResultCode.getMessage());
        r.data = data;
        return r;
    }

    public static <T> Result<T> Maps(Map data) {
        Result r = new Result();
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS);
        r.setMessage("true");
        r.data = data;
        return r;
    }


    /**
     * 根据返回的状态对象， 构建返回结果
     *
     * @param resultEnum
     * @param <T>
     * @return
     */
    public static <T> Result<T> build(ResultEnum resultEnum) {
        return new Result<>(resultEnum);
    }

    /**
     * 根据 code， 和  msg 构建返回结果
     *
     *
     * @param <T>
     * @return
     */
   /* public static <T> Result<T> ok() {
        Result r = new Result();
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS);
        r.setMessage("true");
        return r;
    }*/

    /**
     * 根据 code， 和  msg, 和 data 构建返回结果
     *
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public static <T> Result<T> build(Integer code, String message, T data) {
        return new Result<T>(code, message, data);
    }

    public static <T> Result<T> build(Integer code, String message) {
        return new Result<T>(code, message);
    }

    /**
     * 失败的调用
     *
     * @param
     * @param <T>
     * @return
     */
    public static <T> Result<T> error() {
        Result r = new Result();
        r.setSuccess(false);
        r.setCode(ResultCode.ERROR);
        r.setCode(ResultEnum.ERROR.getCode());
        r.setMessage(ResultEnum.ERROR.getMessage());
        return r;

    }

    public static <T> Result<T> error(Integer code, String message) {
        Result r = new Result();
        r.setSuccess(false);
        r.setCode(code);
        r.setMessage(message);
        return r;

    }

    /**
     * 失败的调用 将返回结果传入
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> error(T data) {
        return new Result<>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMessage(), data);
    }

    public static <T> Result<T> error(CustomizeResultCode resultEnum) {
        return new Result<>(resultEnum.getCode(), resultEnum.getMessage(), null);
    }

    public Result<T> message(String message) {
        this.setMessage(message);
        return this;
    }

    public static Result ok() {
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMessage(ResultEnum.SUCCESS.getMessage());
        return result;
    }

    public static Result errorr(ResultEnum resultCode) {
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        return result;
    }

    public static Result error(ResultCode resultCode) {
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(ResultEnum.ERROR.getCode());
        result.setMessage(ResultEnum.ERROR.getMessage());
        return result;
    }


    private Result(T data) {
        this.code = ResultEnum.SUCCESS.getCode();
        this.message = ResultEnum.SUCCESS.getMessage();
        this.data = data;
    }

    private Result(String msg) {
        this.code = ResultEnum.ERROR.getCode();
        this.data = null;
        this.message = msg;
    }

    private Result(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMessage();
    }

    private Result(Integer code, String msg) {
        this.code = code;
        this.message = msg;
    }

    private Result(Integer code, String msg, T data) {
        this.code = code;
        this.message = msg;
        this.data = data;
    }

    public Result code(Integer code) {
        this.setCode(code);
        return this;
    }


}